package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TMenu;
import com.hopu.blb_erp.entity.TWarehouse;
import com.hopu.blb_erp.mapper.TMenuMapper;
import com.hopu.blb_erp.mapper.WarehouseMapper;
import com.hopu.blb_erp.service.TMenuService;
import com.hopu.blb_erp.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;


@Api
@RestController
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private TMenuService menuService;

    @ApiOperation("分页查询库区")
    @GetMapping("/warehouses/{current}/{pageSize}")
    public ResponseEntity<PageEntity> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize,
                                               Integer menuId, Integer wareId, String date){
        PageEntity<TWarehouse> page = warehouseService.findPage(current, pageSize,menuId,wareId,date);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加库区")
    @PostMapping("/warehouse")
    public ResponseEntity<String> add(@RequestBody TWarehouse warehouse){
        LocalDateTime localDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(localDateTime);
        warehouse.setDateTime(timestamp);
        warehouse.setStatus(true);
        warehouseService.save(warehouse);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除库区")
    @DeleteMapping("/warehouse/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Integer id){
        warehouseService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新库区")
    @PutMapping("/warehouse")
    public ResponseEntity<String> modify(@RequestBody TWarehouse warehouse){
        warehouseService.updateById(warehouse);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改库区状态")
    @PutMapping("/warehouse/{id}/{status}")
    public ResponseEntity<String> update(@PathVariable("id") Integer id,@PathVariable("status")boolean status){
        TWarehouse warehouse = new TWarehouse();
        warehouse.setId(id);
        warehouse.setStatus(status);
        warehouseService.updateById(warehouse);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据仓库id查询库区")
    @GetMapping("/warehouses/{id}")
    public ResponseEntity<List<TWarehouse>> findByMenuId(@PathVariable("id") Integer id){
        List<TWarehouse> warehouses = warehouseService.list(new QueryWrapper<TWarehouse>().lambda().eq(TWarehouse::getWareId, id));
        return ResponseEntity.ok(warehouses);
    }

    @ApiOperation("根据仓库名查询库区")
    @GetMapping("/warehouses")
    public ResponseEntity<List<TWarehouse>> findByMenuName(String menuName){
        TMenu menu = menuService.getOne(new QueryWrapper<TMenu>().lambda().eq(TMenu::getWareName, menuName));
        List<TWarehouse> warehouses = warehouseService.list(new QueryWrapper<TWarehouse>().lambda().eq(TWarehouse::getWareId, menu.getId()));
        return ResponseEntity.ok(warehouses);
    }
}
